import {toast} from "../../utils/tool";

const app = getApp();
Page({
    data: {
        seeDetails: true,
        order_id: 0,
        order: {},
        type: 0,
        isshow: true,
        reorderList: [],
        showPayBtn: false,
    },
    see_details: function () {
        var seeDetails = this.data.seeDetails
        seeDetails = !seeDetails
        this.setData({
            seeDetails: seeDetails
        })
    },

    code: function () {
        wx.navigateTo({
            url: '/pages/pay_success/pay_success?order_id=' + this.data.order_id
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let self = this;
        const {order_id} = options
        this.setData({order_id})
        this.getList(order_id)
        this.getReorderData(order_id)
        if (options.type == 1) {
            this.setData({
                type: options.type
            })
            wx.showModal({
                title: '是否接收',
                success(res) {
                    if (res.cancel) return
                    const hasMobile = app.checkHasMobile()
                    if (!hasMobile) return;
                    self.setData({
                        isshow: false
                    })
                    // self.getList(this.data.order_id);
                    app.ajax({
                        url: '/api/user/transferOrder',
                        data: {
                            order_id: self.data.order_id
                        },
                        success: res => {
                            const {code, msg} = res
                            toast({time: msg})
                        }
                    }, 1);
                }
            })
        }
    },
    onShareAppMessage(res) {
        return {
            title: '亲 赠送给你一个茶室包间请 前去消费',
            path: `/pages/opening_code/opening_code?type=1&order_id=${this.data.order_id}`, //这里是被分享的人点击进来之后的页面
            imageUrl: '' //这里是图片的路径
        }
    },
    formatTime(timestamp) {
        const date = new Date(timestamp)
        const [year, month, day, hour, minute, seconds] = [date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()]

        function patch(value) {
            return ('0' + value).slice(-2)
        }

        return `${year}-${patch(month)}-${patch(day)} ${patch(hour)}:${patch(minute)}:${patch(seconds)}`
    },
    getReorderData(order_id) {
        app.ajax({
            url: "/api/user/orderdetailxudan",
            data: {order_id},
            success: resp => {
                const {data} = resp
                if (data.length) {
                    data.reverse()
                    this.setData({
                        reorderList: data.map(item => {
                            const {create_time, num} = item
                            item.create_time = this.formatTime(create_time * 1000)
                            item.num = num.toFixed(1)
                            return item
                        })
                    })
                }

                console.log('reorder', data)
            }
        })
    },

    getOrderInfo() {

    },
    payHandle() {
        const {id} = this.data.order
        wx.showLoading({title: '正在提交', mask: true})
        app.ajax({
            url: '/api/user/nopaytopay',
            data: {order_id: id},
            success: resp => {
                const {code, data, msg} = resp
                if (code !== 1) {
                    wx.hideLoading()
                    return toast({title: msg})
                }
                if (data.hasOwnProperty('appId')) {
                    wx.requestPayment({
                        ...data,
                        success: ev => {
                            wx.hideLoading()
                            toast({title: '支付成功'}, () => {
                                wx.navigateBack()
                            })
                        },
                        fail: () => {
                            wx.hideLoading()
                            toast({title: '支付失败'})
                        }
                    });
                }
                console.log(resp, 'resp')
            }
        })
    },
    getList: function (order_id) {
        app.ajax({
            url: '/api/user/orderDetail',
            data: {order_id},
            success: res => {
                console.log(res, 'resp')
                const {code, data, msg} = res
                if (code !== 1) return toast({title: msg})
                const {pay_type} = data
                const payTypeEnum = {
                    1: '余额支付',
                    2: '微信支付',
                    3: '美团验券',
                    4:'抖音验券'
                }
                data.pay_type= payTypeEnum[pay_type] || '---'
                this.setData({
                    order_id: order_id,
                    order: data,
                    showPayBtn: data.status * 1 === 10,
                })
            }
        }, 1);
    },
})
